//
//  AppDelegate.m
//  FamilyTravel
//
//  Created by dllo on 15/10/21.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "HYCRecommendController.h"
#import "HYCTourPalController.h"
#import "HYCDestinationController.h"
#import "UMSocial.h"
#import <AFHTTPRequestOperationManager.h>
#import "HYCMemoryTool.h"



#define WIDTH ([UIScreen mainScreen].bounds.size.width/375)
#define HEIGH ([UIScreen mainScreen].bounds.size.height/667)
@interface AppDelegate ()<UITabBarControllerDelegate,UIScrollViewDelegate>
@property (nonatomic, retain) UIPageControl *page;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //分享用的
    
    [HYCMemoryTool isHaveFolder];
    
    [UMSocialData setAppKey:@"5639b440e0f55aa58000115b"];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO];
    
    [self.window makeKeyAndVisible];
    [_window release];
    
    
    NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *strpath = [cachesPath stringByAppendingPathComponent:@"location.txt"];
    
    NSString *str = [NSString stringWithContentsOfFile:strpath encoding:NSUTF8StringEncoding error:nil];
    
    if ([str isEqualToString:@"2"]) {

        self.window.alpha = 0.3;
    } else {
        
        self.window.alpha = 1;

    }

    if (![[NSUserDefaults standardUserDefaults] boolForKey:@"everLaunched"]) {
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"everLaunched"];
        [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"firstLaunch"];
        
        [self createView];
        
        
        UIScrollView *myScroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height)];
        myScroll.contentSize = CGSizeMake(self.window.frame.size.width * 3, self.window.frame.size.height);
        for (int i = 0; i < 3; i++) {
            UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.window.frame.size.width * i, 0, self.window.frame.size.width, self.window.frame.size.height)];
            
            myImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"yindao%d.png", i + 1]];
            [myScroll addSubview:myImage];
            
        }
        myScroll.pagingEnabled = YES;
        myScroll.showsHorizontalScrollIndicator = NO;
        [self.window addSubview:myScroll];
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [myScroll addSubview:button];
        button.frame = CGRectMake(self.window.frame.size.width * 2 + self.window.frame.size.width / 2 - 80 * WIDTH, self.window.frame.size.height - 110 * HEIGH, 150, 40);
        [button addTarget:self action:@selector(myAction) forControlEvents:(UIControlEventTouchUpInside)];
        [button setTitle:@"带着父母去旅游" forState:UIControlStateNormal];
        myScroll.delegate = self;
        
        self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 620 * HEIGH, self.window.frame.size.width, 30)];
        self.page.numberOfPages = 3;
        self.page.currentPage = 0;
        self.page.backgroundColor = [UIColor clearColor];
        self.page.currentPageIndicatorTintColor = [UIColor cyanColor];
        self.page.pageIndicatorTintColor = [UIColor whiteColor];
        [self.window addSubview:self.page];
        
    } else {
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"firstLaunch"];
        [self createView];
    }
    
    
    
    return YES;
}


-(void)getNetWorkNotificationCenter
{
    NSURL *url = [NSURL URLWithString:@"http://www.hao123.com"];
    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url];
    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"4G连接");
                
                UIAlertView *firstAltView = [[UIAlertView alloc] initWithTitle:@"您在使用移动网络" message:@"小心你家房子" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [firstAltView show];
                
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WIFI连接");
                
                break;
            case AFNetworkReachabilityStatusNotReachable: {
                UIAlertView *altView = [[UIAlertView alloc] initWithTitle:@"无网络" message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [altView show];
                [altView release];
                
            } break;
            case AFNetworkReachabilityStatusUnknown:{
                
                UIAlertView *altView = [[UIAlertView alloc] initWithTitle:@"无网络" message:@"请检查网络" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [altView show];
            }
                NSLog(@"连接状态未知");
                
                break;
                
        }
    }];
    
    [manager.reachabilityManager startMonitoring];
    
}



- (void)createView {
    
    HYCRecommendController *recVC = [[HYCRecommendController alloc] init];
    UINavigationController *recNaVC = [[UINavigationController alloc]initWithRootViewController:recVC];
    recNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"推荐" image:[UIImage imageNamed:@"tui.png"] tag:1000] autorelease];
    
    HYCDestinationController *desVC = [[HYCDestinationController alloc] init];
    UINavigationController *desNaVC = [[UINavigationController alloc] initWithRootViewController:desVC];
    desNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"目的地" image:[UIImage imageNamed:@"mudi.png"] tag:1001] autorelease];
    
    
    HYCTourPalController *classVC = [[HYCTourPalController alloc] init];
    UINavigationController *classNaVC = [[UINavigationController alloc] initWithRootViewController:classVC];
    classNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"驴友" image:[UIImage imageNamed:@"lv.png"] tag:1002] autorelease];
    
    ViewController *viVC = [[ViewController alloc]init];
    UINavigationController *viNaVC = [[UINavigationController alloc]initWithRootViewController:viVC];
    viNaVC.tabBarItem = [[[UITabBarItem alloc] initWithTitle:@"订酒店" image:[UIImage imageNamed:@"jiudian.png"] tag:1003] autorelease];
    
    
    
    UITabBarController *tab = [[UITabBarController alloc] init];
    
    tab.viewControllers = @[recNaVC, desNaVC, classNaVC, viNaVC];
    self.window.rootViewController = tab;
    // 设置外观
    // 半通明
    tab.tabBar.translucent = NO;
    // 背景颜色
    //    tab.tabBar.barTintColor = [UIColor yellowColor];
    // 选中的颜色
    tab.tabBar.tintColor = [UIColor blackColor];
    // 打开默认第几个页面
    tab.selectedIndex = 0;
    tab.delegate = self;
    
    [viVC release];
    [desVC release];
    [recVC release];
    [classVC release];
  
    
}




- (void)myAction
{
    [self createView];
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.page.currentPage = scrollView.contentOffset.x / scrollView.window.frame.size.width;
}


- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    
    
    // 点击让红点 消失
    viewController.tabBarItem.badgeValue = nil;
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
